// index.js
import {
    loginOrReg
} from "../../request/request.js"
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
const app = getApp()



Page({
    data: {
        isShowModal: false,
        username: "",
        password: "",
        userInfo: {} //用户信息
    },
    onLoad: function () {


        this.setData({
            userInfo: wx.getStorageSync('userInfo')
        })
    },
    onShow: function () {
        //如果全局数据显示需要弹出登录框
        if (app.globalData.show_login) {
            this.setData({
                isShowModal: true
            })
        }
        //获取当前页面的tabbar对象,设置他的active值为当前页面索引，解决tabbar闪烁乱跳问题
        let tabbarObj = this.getTabBar()
        tabbarObj.setData({
            active: 4
        })
    },

    //显示登录模态框
    showLoginModal() {
        this.setData({
            isShowModal: true
        })
    },
    //用户名输入
    onChangeUsername(e) {
        let username = e.detail.trim()
        if (!username) return
        this.setData({
            username: username
        })

    },
    //密码输入
    onChangePassword(e) {
        let password = e.detail.trim()
        if (!password) return
        this.setData({
            password: password
        })
    },
    //登录逻辑
    loginOrRegFn() {
        const app = getApp()
        if (this.data.username.length <= 0 || this.data.password.length <= 0) {

            app.Notify({
                type: "warning",
                message: '账户或密码不能为空'
            })
            return
        }
        // 弱网环境 加载动画
        wx.showLoading({
            title: '拼命加载中',
        })
        //发起登录请求
        loginOrReg({
            username: this.data.username,
            pwd: this.data.password
        }).then(res => {

            wx.hideLoading()

            if (res.errno != 0) return
            // //关闭加载动画

            app.Notify({
                type: 'success',
                message: '登录成功！！'
            });
            //将全局变量改回默认值
            app.globalData.show_login = false
            wx.setStorageSync("token", res.data.token)
            wx.setStorageSync("userInfo", res.data.userInfo)
            this.setData({
                isShowModal: false,
                username: "",
                password: "",
                userInfo: res.data.userInfo
            })
            //如果有记录需要回跳的页面则执行下面逻辑进行回跳

            let last_page = app.globalData.last_page
            if (!last_page) return
            let arrPages = ["pages/index/index",
                "pages/topic/topic",
                "pages/car/car",
                "pages/categroy/category",
                "pages/user/user",
            ]
            if (arrPages.includes(last_page)) {
                wx.switchTab({
                    url: last_page,
                })


            } else if (last_page == "/pages/product/product") {
                // console.log(app.globalData.options);

                last_page += "?goodsid=" + app.globalData.options
                wx.navigateTo({
                    url: last_page,
                })


            } else {
                wx.navigateTo({
                    url: last_page,
                })

            }
            app.globalData.last_page = ''
        }).catch(err => {})

    },
    //退出登录
    loginOut() {
        const app = getApp()


        Dialog.confirm({
                title: '',
                message: '确定退出登录？',
                selector: "#hello-van-dialog"
            })
            .then(() => {

                wx.clearStorageSync('token')
                wx.clearStorageSync('userInfo')
                this.setData({
                    userInfo: {},
                    username: "",
                    password: ""
                })

                app.Notify({
                    type: "success",
                    message: "退出成功"
                })
            })
            .catch(() => {


            });
    }
})